package com.lixin.smartclassroom.ui.login

import com.drake.net.time.Interval
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.api.getCode
import com.lixin.smartclassroom.api.register
import com.lixin.smartclassroom.databinding.ActivityLoginBinding
import com.lixin.smartclassroom.databinding.ActivityRegisterBinding
import com.lixinkeji.activity.WebActivity
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.extension.commonDialog
import com.lixinkeji.extension.filterPhone
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.start
import com.lixinkeji.extension.text
import com.lixinkeji.extension.toast
import java.util.concurrent.TimeUnit

/**
 * Description 注册
 * Author: lwc
 * Date:2023/12/5 18:02
 */
class RegisterAct : BaseActivity<ActivityRegisterBinding>() {
    override fun getViewBinding() = ActivityRegisterBinding.inflate(layoutInflater)

    override fun topBarHide() = false
    override fun startPadding() = true
    private var phoneNum = ""
    lateinit var interval: Interval

    override fun iniView() {

    }


    override fun title(): String {
        return "注册账号"
    }

    override fun onClick() {
        binding.apply {
            tvRegister.onClick {

                if (!cbAgree.isSelected) {
                    toast("请先阅读并同意用户协议和隐私政策")
                    return@onClick
                }

                if (etPhone.text().isEmpty()) {
                    toast("请输入手机号")
                    return@onClick
                }

                if (etPhone.text().isEmpty() || etPhone.length() != 11) {
                    toast("手机号格式不正确")
                    return@onClick
                }


                if (etCode.text().isEmpty()) {
                    toast("请输入验证码")
                    return@onClick
                }

                if (etPsw.text().isEmpty()) {
                    toast("请输入密码")
                    return@onClick
                }

                if (etPsw2.text().isEmpty()) {
                    toast("请再次输入密码")
                    return@onClick
                }

                if (etPsw.text() != etPsw2.text()) {
                    toast("两次输入密码不一致")
                    return@onClick
                }

                scopeNetLife {
                    val baseBean = register(etPhone.text(),etCode.text(),etPsw.text())
                    if(baseBean.result=="0"){
                        commonDialog(
                            "注册成功",
                            "恭喜您注册成功\n去登录绑定学生信息",
                            "绑定学生",
                            "去登录",
                            onCancel = {
                                start(BindStudentAct::class.java){
                                    putExtra("bindType","1")
                                    putExtra("type","1")
                                }
                            },
                            onSure = {
                                start(LoginActivity::class.java)
                            })
                    }else{
                        toast(baseBean.resultNote.toString())
                    }
                }

            }
            tvToLogin.onClick {
                start(LoginActivity::class.java)
            }
            cbAgree.onClick {
                cbAgree.isSelected = !cbAgree.isSelected
            }

            tvYhxy.onClick {
                start(WebActivity::class.java) {
                    putExtra("title", "用户协议")
                    putExtra("url", "")
                }
            }

            tvYszc.onClick {
                start(WebActivity::class.java) {
                    putExtra("title", "隐私政策")
                    putExtra("url", "")
                }
            }

            tvGetCode.onClick {
                scopeNetLife {
                    phoneNum = etPhone.text?.trim().toString()
                    if (phoneNum.isNullOrEmpty()) {
                        toast("请输入手机号")
                        return@scopeNetLife
                    }
                    tvGetCode.isClickable = false
                    interval = Interval(1, 1, TimeUnit.SECONDS, 60).life(this@RegisterAct)
                    interval.subscribe {
                        tvGetCode.text = it.toString()
                    }.finish {
                        tvGetCode.text = "发送验证码" // 最后一位数时同时回调 subscribe/finish
                        tvGetCode.isClickable = true
                    }.start()
                    getCode(phoneNum)
                }
            }



        }
    }


}